﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using ImageRecognition2.Forms;

using LowLevelGraphics;

namespace ImageRecognition2.Action
{
    /// <summary>
    /// Use this to choose a color
    /// </summary>
    public class ActionChooseColor : AbstractCommand
    {
        private static FormColorSelection formColorSelection = new FormColorSelection();
        private PictureBox m_PictureBox = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="ActionChooseColor"/> class.
        /// </summary>
        /// <param name="_pictureBox">The _picture box.</param>
        public ActionChooseColor(PictureBox _pictureBox)
        {
            m_PictureBox = _pictureBox;
        }
        
        /// <summary>
        /// Runs the specified sender.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        public override void Run(object sender, EventArgs e)
        {
            if (formColorSelection.ShowDialog() == DialogResult.OK)
            {
                UnsafeBitmap bitmap = this.m_PictureBox.BackgroundImage;
                for (int y = 0; y < bitmap.Height; y++)
                {
                    for (int x = 0; x < bitmap.Width; x++)
                    {
                        Color color = bitmap.GetPixel(x, y);
                        if (!ColorFunctions.IsInRange(color, 106, 200, 200, 255, 255, 255))
                        {
                            bitmap.SetPixel(x, y, Color.Black);
                        }
                    }
                }
                bitmap.Dispose();
            }
        }

        /// <summary>
        /// Form Color Selection
        /// </summary>
        public static FormColorSelection FormColorSelection
        {
            get { return formColorSelection; }
        }
    }
}
